package crmsystem.controller;

import crmsystem.entity.ContractEntity;
import crmsystem.feign.BusinessFeignService;
import crmsystem.group.AddGroup;
import crmsystem.group.UpdateGroup;
import crmsystem.service.ContractService;
import crmsystem.utils.PageUtils;
import crmsystem.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author zhugaowei
 * @email 1912431877@qq.com
 * @date 2021-01-07 12:48:11
 */
@RestController
@RequestMapping("/contract")
public class ContractController {
    @Autowired
    private ContractService contractService;

    @Autowired
    BusinessFeignService businessFeignService;

    /**
     * 远程获取商机的所有经办人
     */
    @RequestMapping("/getBusinessGestores")
    public R getBusinessGestores(){
        R businessgestores = businessFeignService.infoByGestores();
        System.out.println(businessgestores);
        return R.ok().put("businessgestores", businessgestores);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("com:contract:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = contractService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("com:contract:info")
    public R info(@PathVariable("id") Integer id){
		ContractEntity contract = contractService.getById(id);

        return R.ok().put("contract", contract);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("com:contract:save")
    public R save(/*@Valid*/@Validated(AddGroup.class) @RequestBody ContractEntity contract){
		contractService.save(contract);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("com:contract:update")
    public R update(/*@Valid*/@Validated(UpdateGroup.class) @RequestBody ContractEntity contract){
		contractService.updateById(contract);

        return R.ok();
    }

    /**
     * 批量删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("com:contract:delete")
    public R delete(@RequestBody Integer[] ids){
		contractService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     *根据id删除
     */
    @RequestMapping("/delete/{id}")
    //@RequiresPermissions("com:business:delete")
    public R deleteById(@PathVariable("id") Integer id){
        contractService.removeById(id);
        return R.ok();
    }

    /**
     * 多字段模糊查询
     */
    @RequestMapping("/infoByIndex/{index}")
    //@RequiresPermissions("com:contactperson:info")
    public R infoByIndex(@PathVariable("index") String index){
        List<ContractEntity> entities = contractService.getByIndex(index);
        return R.ok().put("entities", entities);
    }
}
